package com.example.yt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 先根据签约状态排序 已签约 已失效 已解约
 * 整体根据生效日期排序（日期最新排前面）
 * 生效日期同一天内的用签约时间排序
 *
 */
public class SortBySignAndEffectDate {
    public static void main(String[] args) {
        List<SignData> originList = initOriginList();

        List<SignData> result = handleOriginData(originList);

        for (SignData data :
                result) {
            System.out.println(data.toString());
        }
    }

    /**
     * 处理一下List 列表数据
     *
     * @param originList 原始列表数据
     * @return List
     */
    private static List<SignData> handleOriginData(List<SignData> originList) {
        List<SignData> result = new ArrayList<>(); // 返回的结果集
        List<SignData> signedList = new ArrayList<>(); // 已签约
        List<SignData> disEffect = new ArrayList<>(); // 已失效
        List<SignData> canceledList = new ArrayList<>(); // 已解约

        for (int i = 0; i < originList.size(); i++) {
            if (originList.get(i).getSignFlag().equals("SIGN")) {
                signedList.add(originList.get(i));
            }else if (originList.get(i).getSignFlag().equals("DISSECT")) {
                disEffect.add(originList.get(i));
            }else if (originList.get(i).getSignFlag().equals("CANCEL")) {
                canceledList.add(originList.get(i));
            }
        }

        // 已签约数据按生效日期排序
        signedList = sortListByEffectDate(signedList);
        disEffect = sortListByEffectDate(disEffect);
        canceledList = sortListByEffectDate(canceledList);

        result.addAll(signedList);
        result.addAll(disEffect);
        result.addAll(canceledList);
        return result;
    }

    /**
     * 按生效日期排序
     *
     * @param signedList 已签约列表数据
     * @return list
     */
    private static List<SignData> sortListByEffectDate(List<SignData> signedList) {
        if (signedList != null && signedList.size() > 0) {
            // 如果生效日期一致，则按照签约时间排序
            Collections.sort(signedList, (o1, o2) -> {
                if (o1.getEffectDate().equals(o2.getEffectDate())){
                    Long signTime1 = Long.valueOf(o1.getSignTime());
                    Long signTime2 = Long.valueOf(o2.getSignTime());
                    return signTime2.compareTo(signTime1);
                }else {
                    Long effectDate1 = Long.valueOf(o1.getEffectDate());
                    Long effectDate2 = Long.valueOf(o2.getEffectDate());
                    return effectDate2.compareTo(effectDate1);
                }
            });
        }
        return signedList;
    }

    /**
     * 生成原始模拟数据
     * @return List
     */
    private static List<SignData> initOriginList() {
        List<SignData> mapList = new ArrayList<>();
        // 已签约
        for (int i = 1; i < 10; i++) {
            if (i % 2 == 0) {
                SignData signData = new SignData(System.currentTimeMillis(), "20240101", "01", "SIGN");
                stopOneSeconds();
                mapList.add(signData);
            }
            SignData signData = new SignData(System.currentTimeMillis(), "2024010" + i, "01", "SIGN");
            stopOneSeconds();
            mapList.add(signData);
        }

        // 已失效
        for (int i = 1; i < 10; i++) {
            if (i % 2 == 0) {
                SignData signData = new SignData(System.currentTimeMillis(), "20240102", "03", "DISSECT");
                stopOneSeconds();
                mapList.add(signData);
            }
            SignData signData = new SignData(System.currentTimeMillis(), "2024010" + i, "03", "DISSECT");
            stopOneSeconds();
            mapList.add(signData);
        }

        // 已解约
        for (int i = 1; i < 10; i++) {
            if (i % 2 == 0) {
                SignData signData = new SignData(System.currentTimeMillis(), "20240103", "02", "CANCEL");
                stopOneSeconds();
                mapList.add(signData);
            }
            SignData signData = new SignData(System.currentTimeMillis(), "2024010" + i, "02", "CANCEL");
            stopOneSeconds();
            mapList.add(signData);
        }
        return mapList;
    }

    /**
     * 暂停400ms 为了签约时间不一样
     */
    private static void stopOneSeconds() {
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

/**
 * 签约数据对象
 */
class SignData{
    private long signTime; // 签约时间

    private String effectDate;// 生效日期

    private String signStatus; // 签约状态

    private String signFlag;  // 签约标记

    public SignData(long signTime, String effectDate, String signStatus, String signFlag) {
        this.signTime = signTime;
        this.effectDate = effectDate;
        this.signStatus = signStatus;
        this.signFlag = signFlag;
    }

    public long getSignTime() {
        return signTime;
    }

    public void setSignTime(long signTime) {
        this.signTime = signTime;
    }

    public java.lang.String getEffectDate() {
        return effectDate;
    }

    public void setEffectDate(java.lang.String effectDate) {
        this.effectDate = effectDate;
    }

    public String getSignStatus() {
        return signStatus;
    }

    public void setSignStatus(String signStatus) {
        this.signStatus = signStatus;
    }

    public String getSignFlag() {
        return signFlag;
    }

    public void setSignFlag(String signFlag) {
        this.signFlag = signFlag;
    }

    @Override
    public String toString() {
        return "SignData{" +
                "signTime=" + signTime +
                ", effectDate='" + effectDate + '\'' +
                ", signStatus='" + signStatus + '\'' +
                ", signFlag='" + signFlag + '\'' +
                '}';
    }
}
